package com.ruoyi.yp.controller;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.yp.domain.vo.BsYpVo;
import com.ruoyi.yp.domain.bo.BsYpBo;
import com.ruoyi.yp.service.IBsYpService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 样品
 *
 * @author zyb
 * @date 2024-03-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/yp/bsYp")
public class BsYpController extends BaseController {

    private final IBsYpService iBsYpService;

    /**
     * 查询样品列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<BsYpVo> list(BsYpBo bo, PageQuery pageQuery) {
        return iBsYpService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出样品列表
     */
    @SaCheckLogin
    @Log(title = "样品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BsYpBo bo, HttpServletResponse response) {
        List<BsYpVo> list = iBsYpService.queryList(bo);
//        ExcelUtil.exportExcel(list, "样品", BsYpVo.class, response);
        IntStream.range(0, list.size()).forEach(i ->{
            list.get(i).setRowNumber(i + 1);
        });
        Map<String, String> map = new HashMap<>();
        ExcelUtil.exportTemplate(CollUtil.newArrayList(map,list), "样品取样登记表.xlsx", "excel/样品.xlsx", response);
    }


    /**
     * 获取样品详细信息
     *
     * @param ypId 主键
     */
    @SaCheckLogin
    @GetMapping("/{ypId}")
    public R<BsYpVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ypId) {
        return R.ok(iBsYpService.queryById(ypId));
    }

    /**
     * 新增样品
     */
    @SaCheckLogin
    @Log(title = "样品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BsYpBo bo) {
        return toAjax(iBsYpService.insertByBo(bo));
    }

    /**
     * 修改样品
     */
    @SaCheckLogin
    @Log(title = "样品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BsYpBo bo) {
        return toAjax(iBsYpService.updateByBo(bo));
    }

    /**
     * 删除样品
     *
     * @param ypIds 主键串
     */
    @SaCheckLogin
    @Log(title = "样品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ypIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ypIds) {
        return toAjax(iBsYpService.deleteWithValidByIds(Arrays.asList(ypIds), true));
    }
}
